package com.express.e.root.dao.impl;

import com.express.e.root.bean.User;
import com.express.e.root.dao.UserDao;
import com.express.e.root.utils.DBUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("all")
public class UserDaoImpl extends DBUtils implements UserDao {
    @Override
    public Boolean addUser(User user) {
        Integer addNum = 0;
        String sql = "INSERT INTO USER (userName,userPhone,userPwd,cardId,userRegisterTime,userLastLoginTime) VALUES (?,?,?,?,?,?);";
        ArrayList<Object> params = new ArrayList<>();
        params.add(user.getUserName());
        params.add(user.getUserPhone());
        params.add(user.getUserPwd());
        params.add(user.getCardId());
        params.add(user.getUserRegisterTime());
        params.add(user.getUserLastLoginTime());
        try {
            addNum = update(sql, params);
        } finally {
            closeAll();
        }
        return addNum > 0;
    }

    @Override
    public Boolean deleteUser(Integer userId) {
        Integer delNum = 0;
        String sql = "UPDATE USER AS u SET u.`isDeleted` = 1 WHERE u.`userId` = ?;";
        ArrayList<Object> params = new ArrayList<>();
        params.add(userId);
        try {
            delNum = update(sql, params);
        } finally {
            closeAll();
        }
        return delNum > 0;
    }

    @Override
    public Boolean updateUser(User user) {
        Integer updateNum = 0;
        String sql = "UPDATE USER AS u SET u.`userName`=?,u.`userPhone`=?,u.`userPwd`=?,u.`cardId`=?,u.`userRegisterTime`=?,u.`userLastLoginTime`=? WHERE u.`userId` = ?;";
        ArrayList<Object> params = new ArrayList<>();
        params.add(user.getUserName());
        params.add(user.getUserPhone());
        params.add(user.getUserPwd());
        params.add(user.getCardId());
        params.add(user.getUserRegisterTime());
        params.add(user.getUserLastLoginTime());
        params.add(user.getUserId());
        try {
            updateNum = update(sql, params);
        } finally {
            closeAll();
        }
        return updateNum > 0;
    }

    @Override
    public List listUser() {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "select * from user as u where u.`isDeleted` = 0";
        try {
            resultSet = query(sql, null);
            while (resultSet.next()) {
                User user = new User(
                        resultSet.getInt("userId"),
                        resultSet.getString("userName"),
                        resultSet.getString("userPhone"),
                        resultSet.getString("userPwd"),
                        resultSet.getString("cardId"),
                        resultSet.getDate("userRegisterTime"),
                        resultSet.getDate("userLastLoginTime"),
                        resultSet.getInt("isSaveWeekPwd")
                );
                list.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return list;
    }

    @Override
    public User queryById(Integer userId) {
        User user = null;
        String sql = "select * from user where isDeleted = 0 and userId = ?;";
        ArrayList<Object> params = new ArrayList<>();
        params.add(userId);
        try {
            resultSet = query(sql, params);
            while (resultSet.next()) {
                user = new User(
                        resultSet.getInt("userId"),
                        resultSet.getString("userName"),
                        resultSet.getString("userPhone"),
                        resultSet.getString("userPwd"),
                        resultSet.getDate("userRegisterTime"),
                        resultSet.getDate("userLastLoginTime"),
                        resultSet.getInt("isSaveWeekPwd")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return user;
    }

    @Override
    public User queryByName(String userName) {
        User user = null;
        String sql = "select * from user where isDeleted = 0 and userName = ?;";
        ArrayList<Object> params = new ArrayList<>();
        params.add(userName);
        try {
            resultSet = query(sql, params);
            while (resultSet.next()) {
                user = new User(
                        resultSet.getInt("userId"),
                        resultSet.getString("userName"),
                        resultSet.getString("userPhone"),
                        resultSet.getString("userPwd"),
                        resultSet.getString("cardId"),
                        resultSet.getDate("userRegisterTime"),
                        resultSet.getDate("userLastLoginTime"),
                        resultSet.getInt("isSaveWeekPwd")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return user;
    }

    @Override
    public User queryByPhone(String phone) {
        User user = null;
        String sql = "select * from user where userPhone = ? and isDeleted =0;";
        ArrayList<Object> params = new ArrayList<>();
        params.add(phone);
        try {
            resultSet = query(sql, params);
            while (resultSet.next()){
                user = new User(
                        resultSet.getInt("userId"),
                        resultSet.getString("userName"),
                        resultSet.getString("userPhone"),
                        resultSet.getString("userPwd"),
                        resultSet.getString("cardId"),
                        resultSet.getDate("userRegisterTime"),
                        resultSet.getDate("userLastLoginTime"),
                        resultSet.getInt("isSaveWeekPwd")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return user;
    }

    @Override
    public User queryByCardId(String cardId) {
        User user = null;
        String sql = "select * from user where cardId = ? and isDeleted =0;";
        ArrayList<Object> params = new ArrayList<>();
        params.add(cardId);
        try {
            resultSet = query(sql, params);
            while (resultSet.next()){
                user = new User(
                        resultSet.getInt("userId"),
                        resultSet.getString("userName"),
                        resultSet.getString("userPhone"),
                        resultSet.getString("userPwd"),
                        resultSet.getString("cardId"),
                        resultSet.getDate("userRegisterTime"),
                        resultSet.getDate("userLastLoginTime"),
                        resultSet.getInt("isSaveWeekPwd")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return user;
    }
}
